var app = getApp()


/**
 * 地址
 */
// const domainName = 'http://www.test5.com';//本地地址
const domainName = 'https://api.chenxiaoxiang.top'; //线上地址

function url() {
  return domainName;
}

/**
 * 封装wx.request
 *Function：
 *  POST方式:request
 *  GET方式:getrequest
 * 参数：
 *  {string}    url   路由
 *  {Object}    data  data
 *  {function}  cb    回调（callback）
 * 
 */
function getrequest(url, data, cb) {
  let user_id = wx.getStorageSync('user_id')
  var cmmnData = {} //公用参数(扩展)
  if(user_id){
    cmmnData.user_id = user_id;
  }
  data = Object.assign(data, cmmnData)
  //发送请求
  wx.request({
    url: domainName + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json;charset=UTF-8',
    },
    method: 'GET',
    dataType: 'json',
    success: function (res) {
      cb(res.data)
    },
    fail: function (res) {
      console.log('接口请求失败')
    }
  })
}

function request(url, data, cb) {
  let user_id = wx.getStorageSync('user_id')
  var cmmnData = {} //公用参数(扩展)
  if (user_id) {
    cmmnData.user_id = user_id;
  }
  data = Object.assign(data, cmmnData)
  //发送请求
  wx.request({
    url: domainName + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json;charset=UTF-8'
    },
    method: 'POST',
    dataType: 'json',
    success: function (res) {
      cb(res.data)
    },
    fail: function (res) {
      console.log('接口请求失败')
    }
  })
}




module.exports = {
  url: url,
  request: request,
  getrequest: getrequest,
}